package conservare.modelo.servico;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import conservare.modelo.ItemDeDiaSemana;
import conservare.modelo.contrato.Contrato;
import conservare.modelo.type.DiaSemana;


@Entity
@Table(name="TB_SERV_PRINC")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="discriminator_column", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("DESCONHECIDO")
public class ServicoPrincipal implements Serializable{

	private static final long serialVersionUID = 1569838684898148001L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="SERV_PRINC_ID")
	protected Integer id;
	
	@OneToOne(targetEntity=Contrato.class)
	@JoinColumn(name="CONTRATO_ID")
	protected Contrato contrato;
	
	@OneToMany(mappedBy="servicoPrincipal", cascade = CascadeType.ALL)
	protected List<ItemDeDiaSemana> agendaSemanal = new ArrayList<>();
	
	
	public void adicionarDiaSemanaNaAgenda(DiaSemana diaSemana, Date qtdHorasDia){
		
		if(isDiaSemanaExisteNaAgenda(diaSemana)){
			obterItemDeDiaSemanaDesteDia(diaSemana).setQtdHorasDia(qtdHorasDia);
		}else{
			ItemDeDiaSemana itemDeDiaSemana = new ItemDeDiaSemana(this, diaSemana, qtdHorasDia);
			getAgendaSemanal().add(itemDeDiaSemana);
		}

	}
	
	public void removerDiaSemanaDaAgenda(ItemDeDiaSemana itemDeDiaSemana){
		if(getAgendaSemanal().size() > 0){
			getAgendaSemanal().remove(itemDeDiaSemana);
		}
	}

	private boolean isDiaSemanaExisteNaAgenda(DiaSemana diaSemana){
		
		boolean diaSemanaExisteNaLista = false;
		
		for(ItemDeDiaSemana itemDeDiaSemana : getAgendaSemanal()){
			if(itemDeDiaSemana.getDiaSemana() == diaSemana){
				diaSemanaExisteNaLista = true;
				break;
			}
		}
		
		return diaSemanaExisteNaLista;
		
	}
	
	private ItemDeDiaSemana obterItemDeDiaSemanaDesteDia(DiaSemana diaSemana){
		
		ItemDeDiaSemana itemDeDiaSemanaLocalizado = null;
		
		for(ItemDeDiaSemana itemDeDiaSemana : getAgendaSemanal()){
			if(itemDeDiaSemana.getDiaSemana() == diaSemana){
				itemDeDiaSemanaLocalizado = itemDeDiaSemana;
				break;
			}
		}
		
		return itemDeDiaSemanaLocalizado;
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public List<ItemDeDiaSemana> getAgendaSemanal() {
		return agendaSemanal;
	}

	public void setAgendaSemanal(List<ItemDeDiaSemana> agendaSemanal) {
		this.agendaSemanal = agendaSemanal;
	}

	public void setContrato(Contrato contrato) {
		this.contrato = contrato;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ServicoPrincipal other = (ServicoPrincipal) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}
	
	
}
